﻿//                              \\\|///                              
//                            \\       //                            
//                             (  @.@  )                             
//+----------------------oOOo-----(_)-----oOOo------------------------+
//|                                                                   |
//|                    Created by stg609 　　　　　　　　　　　　　　 |
//|                                                                   |
//|               本人博客：http://stg609.cnblogs.com                 |
//|         由于水平有限，所写代码若有不足，欢迎大家到我博客交流      |
//|                                                                   |
//|                    注:转载请保留此信息                            |
//|                                                                   |
//|                 Copyright (C) 2008 stg609                         |
//|                     All rights reserved                           |
//|                                                                   |
//+-------------------+-ooO------------------Ooo-+--------------------+
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Collections;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace MatureVoice
{
    public partial class Voice : DevComponents.DotNetBar.Office2007Form
    {
        public Voice()
        {
            InitializeComponent();
        }

        private Point posMouse;
        private NetChat netchat1;

        private void Voice_Load(object sender, EventArgs e)
        {
            netchat1 = new NetChat(8000);
            this.Text = "本机：" + netchat1.BindSelf(netchat1.LocalIPEnd);
            this.comboIP.Text = Dns.GetHostAddresses(Dns.GetHostName())[0].ToString();
        }

        private void Voice_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                posMouse.X = e.X;
                posMouse.Y = e.Y;
            }
        }

        private void Voice_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Voice.ActiveForm.Top = Control.MousePosition.Y - posMouse.Y - SystemInformation.FrameBorderSize.Height - SystemInformation.CaptionHeight;
                Voice.ActiveForm.Left = Control.MousePosition.X - posMouse.X - SystemInformation.FrameBorderSize.Width;

            }
        }
       
        private void btnSelect_Click(object sender, EventArgs e)
        {
            FormWrite("正在查找局域网内主机，请稍后...");
            Form2 frm2 = new Form2();
            if (frm2.ShowDialog() == DialogResult.OK)
            {
                comboIP.Text = frm2.IPAdd;
                FormWrite("已经找到可用主机");
            }
            else
            {
                FormWrite("没有找到合适的主机...");
            }
            frm2.Close();
            

        }



        //建立一个委托
        private delegate void FormHandel(string strArgu);

        private void FormWrite(string strText)
        {
            if (InvokeRequired)
            {
                FormHandel handel1 = new FormHandel(FormWrite);
                this.Invoke(handel1, new object[] { strText });
            }
            else
            {
                lblStatus.Text = strText;
            }
        }

        private void Voice_FormClosing(object sender, FormClosingEventArgs e)
        {

        }

        private void lblStatus_TextChanged(object sender, EventArgs e)
        {
            timer1.Start();
        }
        private int intTickedTimes = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            intTickedTimes++;
            if (intTickedTimes > 6)
            {
                timer1.Stop();
                intTickedTimes = 0;
            }
            else
            {
                lblStatus.ForeColor = intTickedTimes % 2 == 0 ? Color.MidnightBlue : Color.Red;
            }
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                netchat1.SetRemoteIPEnd(comboIP.Text, 8000);
                btnChat.Enabled = true;
                FormWrite("本机已经准备就绪！");
            }
            catch
            {
                FormWrite("出错了，IP地址非法");
                btnChat.Enabled = false;
            }
        }

        private void btnChat_Click(object sender, EventArgs e)
        {//先初始化语音模块，启动监听方法，再启动语音采集方法
            if (btnChat.Text == "开始语聊")
            {
                btnChat.Text = "停止语聊";
                btnSelect.Enabled = false;
                btnSubmit.Enabled = false;
                try
                {
                    netchat1.Intptr = this.Handle;
                    netchat1.InitVoice();
                }
                catch
                {
                    FormWrite("声音模块初始化失败");
                }
                try
                {
                    netchat1.Listen();
                }
                catch
                {
                    FormWrite("监听模块异常");
                }
                try
                {
                    netchat1.StartSendVoice();
                }
                catch
                {
                    if (MessageBox.Show("应用程序出现问题，需要重新启动应该程序嘛？", "系统提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        Application.Exit();//先关闭应用程序
                        System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    }

                }
            }
            else
            {
                netchat1.Stop();
                btnChat.Text = "开始语聊";
                btnSelect.Enabled = true;
                btnSubmit.Enabled = true;
            }
        }
    }
}